/**
 * 自定义函数
 * */


//场景列表
function getIndex(){
    var formData = {
        pageStart: pages.start,
        pageLimit: pages.limit,
    };
    console.log(formData);
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'mall/goods/list',
        data: formData,
        success: function (res) {
            console.log(res);
            var h = '', tbody = $('tbody');
            if (res.code != 0) {
                h = '<tr class="text-c">';
                h += '<td colspan="10">没有记录</td>';
                h += '</tr>';
                tbody.html(h);
                return
            }
            formList = res.data;
            tbody.html(getHtml(res.data));
            $('#pageTotal').html(res.total);
            pages.total = res.total;
            pageHtml(pages);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}
//场景列表字符串
function getHtml(d){
    var h = '', i;
    h += '';
    for (i = 0; i < d.length; i ++) {
        h += '<tr class="text-c">';
        h += '<td>' + d[i].id + '</td>';
        h += '<td>' + d[i].title + '</td>';
        h += '<td>' + d[i].cate_name + '</td>';
        h += '<td>' + d[i].shop_name + '</td>';
        if (d[i].cover == '') {
            h += '<td>-</td>';
        } else {
            h += '<td>';
            h += '<a href="/' + d[i].cover + '" target="_blank">';
            h += '<img src="/' + d[i].cover + '" width="100"/>';
            h += '</a>';
            h += '</td>';
        }
        h += '<td class="td-manage">';
        h += '<a href="javascript:;" class="ml-5" onclick="lay_edit(\'编辑\',\'goods-edit.html\', ' + d[i].id + ');">';
        h += '编辑';
        h += '<i class="Hui-iconfont">&#xe6df;</i>';
        h += '</a>';
        h += '&nbsp;&nbsp;';
        h += '<a href="javascript:;" class="ml-5" onclick="lay_del(' + d[i].id + ');" style="color: red;">';
        h += ' 删除';
        h += '<i class="Hui-iconfont"></i>';
        h += '</a>';
        h += '<input type="hidden" id="title' + d[i].id + '" value="' + d[i].title + '"/>';
        h += '<input type="hidden" id="cover' + d[i].id + '" value="' + d[i].cover + '"/>';
        h += '<input type="hidden" id="cateid' + d[i].id + '" value="' + d[i].cate_id + '"/>';
        h += '<input type="hidden" id="shopid' + d[i].id + '" value="' + d[i].shop_id + '"/>';
        h += '<input type="hidden" id="price' + d[i].id + '" value="' + d[i].price + '"/>';
        h += '<input type="hidden" id="price_old' + d[i].id + '" value="' + d[i].price_old + '"/>';
        h += '<input type="hidden" id="img01' + d[i].id + '" value="' + d[i].img01 + '"/>';
        h += '<input type="hidden" id="img02' + d[i].id + '" value="' + d[i].img02 + '"/>';
        h += '<input type="hidden" id="img03' + d[i].id + '" value="' + d[i].img03 + '"/>';
        h += '<input type="hidden" id="img04' + d[i].id + '" value="' + d[i].img04 + '"/>';
        h += '</td>';
        h += '</tr>';
    }
    return h;
}


//编辑弹窗
function lay_edit(title,url,id,w,h){
    url += '?id=' + id;
    var tt = $('#title' + id).val();
    var cover = $('#cover' + id).val();
    var cate_id = $('#cateid' + id).val();
    var shop_id = $('#shopid' + id).val();
    var price = $('#price' + id).val();
    var price_old = $('#price_old' + id).val();
    var img01 = $('#img01' + id).val();
    var img02 = $('#img02' + id).val();
    var img03 = $('#img03' + id).val();
    var img04 = $('#img04' + id).val();
    tt = tt ? escape(tt) : '';
    cover = cover ? cover : '';
    cate_id = cate_id ? cate_id : 0;
    shop_id = shop_id ? shop_id : 0;
    price = price ? price : 0;
    price_old = price_old ? price_old : 0;
    img01 = img01 ? img01 : '';
    img02 = img02 ? img02 : '';
    img03 = img03 ? img03 : '';
    img04 = img04 ? img04 : '';
    url += '&title=' + tt;
    url += '&cover=' + cover;
    url += '&cate_id=' + cate_id;
    url += '&shop_id=' + shop_id;
    url += '&price=' + price;
    url += '&price_old=' + price_old;
    url += '&img01=' + img01;
    url += '&img02=' + img02;
    url += '&img03=' + img03;
    url += '&img04=' + img04;
    layer_show(title,url,w,h);
}


//获取详情
function getOne(){
    if (!id) { return }
    $('#title')[0].value = title;
    $('#price')[0].value = price;
    $('#price_old')[0].value = price_old;
    var h = '', i;
    if (cover != '') {
        h = '<img src="/' + cover + '"/>';
        h += '<input type="hidden" id="img" value="' + cover + '"/>';
        $('#bigShow').html(h);
    }
    if (img01 != '') {
        h = '<img src="/' + img01 + '"/>';
        h += '<input type="hidden" id="img01" value="' + img01 + '"/>';
        $('#xq01Show').html(h);
    }
    if (img02 != '') {
        h = '<img src="/' + img02 + '"/>';
        h += '<input type="hidden" id="img02" value="' + img02 + '"/>';
        $('#xq02Show').html(h);
    }
    if (img03 != '') {
        h = '<img src="/' + img03 + '"/>';
        h += '<input type="hidden" id="img03" value="' + img03 + '"/>';
        $('#xq03Show').html(h);
    }
    if (img04 != '') {
        h = '<img src="/' + img04 + '"/>';
        h += '<input type="hidden" id="img04" value="' + img04 + '"/>';
        $('#xq04Show').html(h);
    }
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'mall/goods/option',
        data: { cate_id: cate_id, shop_id: shop_id },
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                //msgPop({msg: res.msg});
                return
            }
            catelist = res.data.cate;
            shoplist = res.data.shop;
            var h = '', i;
            //分类
            if (!cate_id) {
                h = '<option value="">-选择-</option>';
            } else {
                h = '<option value="" selected>-选择-</option>';
            }
            h = '<option value="">-选择-</option>';
            for (i = 0; i < catelist.length; i ++) {
                if (cate_id == catelist[i].id) {
                    h += '<option value="' + catelist[i].id + '" selected>';
                } else {
                    h += '<option value="' + catelist[i].id + '">';
                }
                h += catelist[i].title;
                h += '</option>';
            }
            $('#cate_id').html(h);
            //门店
            if (!shop_id) {
                h = '<option value="" selected>-选择-</option>';
            } else {
                h = '<option value="">-选择-</option>';
            }
            for (i = 0; i < shoplist.length; i ++) {
                if (shop_id == shoplist[i].id) {
                    h += '<option value="' + shoplist[i].id + '" selected>';
                } else {
                    h += '<option value="' + shoplist[i].id + '">';
                }
                h += shoplist[i].title;
                h += '</option>';
            }
            $('#shop_id').html(h);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//显示图片
function showImg(e){
    var h = '';
    h += '<img src="' + e.img + '" style="width: 150px;"/>';
    h += '<input type="hidden" id="img" value="' + e.img + '"/>';
    $('#bigShow').html(h);
}


//详情01抓图
function xqImg01() {
    var fileObj = $('#xq01Upload')[0],
        reader = new FileReader();
    reader.readAsDataURL(fileObj.files[0]); //这一步是关键
    reader.onload = function (res){
        var h = '', img = res.target.result;
        h += '<img src="' + img + '" style="width: 150px;"/>';
        h += '<input type="hidden" id="img01" value="' + img + '"/>';
        $('#xq01Show').html(h);
    }
}


//详情01抓图
function xqImg02() {
    var fileObj = $('#xq02Upload')[0],
        reader = new FileReader();
    reader.readAsDataURL(fileObj.files[0]); //这一步是关键
    reader.onload = function (res){
        var h = '', img = res.target.result;
        h += '<img src="' + img + '" style="width: 150px;"/>';
        h += '<input type="hidden" id="img02" value="' + img + '"/>';
        $('#xq02Show').html(h);
    }
}


//详情01抓图
function xqImg03() {
    var fileObj = $('#xq03Upload')[0],
        reader = new FileReader();
    reader.readAsDataURL(fileObj.files[0]); //这一步是关键
    reader.onload = function (res){
        var h = '', img = res.target.result;
        h += '<img src="' + img + '" style="width: 150px;"/>';
        h += '<input type="hidden" id="img03" value="' + img + '"/>';
        $('#xq03Show').html(h);
    }
}


//详情04抓图
function xqImg04() {
    var fileObj = $('#xq04Upload')[0],
        reader = new FileReader();
    reader.readAsDataURL(fileObj.files[0]); //这一步是关键
    reader.onload = function (res){
        var h = '', img = res.target.result;
        h += '<img src="' + img + '" style="width: 150px;"/>';
        h += '<input type="hidden" id="img04" value="' + img + '"/>';
        $('#xq04Show').html(h);
    }
}


//选项
function option(){
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'mall/goods/option',
        data: {},
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                //msgPop({msg: res.msg});
                return
            }
            catelist = res.data.cate;
            shoplist = res.data.shop;
            var h = '', i;
            //分类
            h = '<option value="">-选择-</option>';
            for (i = 0; i < catelist.length; i ++) {
                h += '<option value="' + catelist[i].id + '">';
                h += catelist[i].title;
                h += '</option>';
            }
            $('#cate_id').html(h);
            //门店
            h = '<option value="">-选择-</option>';
            for (i = 0; i < shoplist.length; i ++) {
                h += '<option value="' + shoplist[i].id + '">';
                h += shoplist[i].title;
                h += '</option>';
            }
            $('#shop_id').html(h);

        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//修改
function modify(){
    var formData = {
        id: id,
        title: $('#title').val(),
        shop_id: $('#shop_id').val(),
        cate_id: $('#cate_id').val(),
        price: $('#price').val(),
        price_old: $('#price_old').val(),
        img: $('#img').val(),
        img01: $('#img01').val(),
        img02: $('#img02').val(),
        img03: $('#img03').val(),
        img04: $('#img04').val(),
    };
    formData.img = formData.img ? formData.img : '';
    formData.img01 = formData.img01 ? formData.img01 : '';
    formData.img02 = formData.img02 ? formData.img02 : '';
    formData.img03 = formData.img03 ? formData.img03 : '';
    formData.img04 = formData.img04 ? formData.img04 : '';
    console.log(formData);
    if (!formData.title) {
        msgPop({msg: '商品名必须的'}); return
    }
    if (!formData.shop_id) {
        msgPop({msg: '门店必须的'}); return
    }
    if (!formData.cate_id) {
        msgPop({msg: '分类必须的'}); return
    }
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'mall/goods/modify',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                //msgPop({msg: res.msg});
                return
            }
            msgPop({msg: '操作成功'});
            //window.location.href = '';
            setTimeout(function(){
                top.location.reload();
            }, 500);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//删除
function lay_del(id) {
    var msg = '确认要 <b style="color: red; font-size: 20px;">删除</b> 吗？';
    layer.confirm(msg, function (index) {
        $.ajax({
            type: apiMethod,
            url: apiDomain + 'mall/goods/del',
            data: {aid: getAid(), id: id},
            success: function (res) {
                console.log(res);
                if (res.code < 0) {
                    msgPop({msg: res.msg});
                    return
                }
                layer.msg('已删除!', {icon: 1, time: 1000});
                setTimeout(function () {
                    top.location.reload();
                }, 500);
            },
            error: function () {
                console.log('错误了！');
            }
        });
    });
}


//内容弹窗
function lay_cnt(title,url,id,w,h){
    url += '?scene_id=' + id;
    layer_show(title,url,w,h);
}
